<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - PCD</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				font-family: Monospace;
				background-color: #000000;
				margin: 0px;
				overflow: hidden;
			}

			#info {
				color: #fff;
				position: absolute;
				top: 10px;
				width: 100%;
				text-align: center;
				z-index: 100;
				display:block;

			}

			a { color: #d14826 }
			.button { background:#999; color:#eee; padding:0.2em 0.5em; cursor:pointer }
			.highlight { background:orange; color:#fff; }

			span {
				display: inline-block;
				width: 60px;
				float: left;
				text-align: center;
			}

		</style>
	</head>
	<body>
		<div id="info">
			<a href="http://threejs.org" target="_blank">three.js</a>
			<a href="http://pointclouds.org/documentation/tutorials/pcd_file_format.php#pcd-file-format" target="_blank">PCD File format</a>
			<div>PCD loader test by <a href="http://filipecaixeta.com.br" target="_blank">Filipe Caixeta</a></div>
			<div>+/-: Increase/Decrease point size</div>
			<div>c: Toggle color</div>
		</div>


		<script src="../build/three.js"></script>
		<script src="js/loaders/PCDLoader.js"></script>
		<script src="js/controls/TrackballControls.js"></script>
		<script src="js/Detector.js"></script>
		<script src="js/libs/stats.min.js"></script>

		<script>

			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

			var container, stats;
			var camera, controls, scene, renderer;

			init();
			animate();
			function init() {

				scene = new THREE.Scene();

				camera = new THREE.PerspectiveCamera( 15, window.innerWidth / window.innerHeight, 0.01, 40 );
				camera.position.x = 0.4;
				camera.position.z = -2;
				camera.up.set(0,0,1);

				controls = new THREE.TrackballControls( camera );

				controls.rotateSpeed = 2.0;
				controls.zoomSpeed = 0.3;
				controls.panSpeed = 0.2;

				controls.noZoom = false;
				controls.noPan = false;

				controls.staticMoving = true;
				controls.dynamicDampingFactor = 0.3;

				controls.minDistance = 0.3;
				controls.maxDistance = 0.3 * 100;

				scene.add( camera );

				var axisHelper = new THREE.AxisHelper( 0.1 );
				scene.add( axisHelper );

				renderer = new THREE.WebGLRenderer( { antialias: true } );
				renderer.setClearColor( 0x000000 );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				document.body.appendChild( renderer.domElement );

				var loader = new THREE.PCDLoader();
				loader.load( './models/pcd/Zaghetto.pcd', function ( mesh ) {

					scene.add( mesh );
					var center = mesh.geometry.boundingSphere.center;
					controls.target.set( center.x, center.y, center.z);
					controls.update();

				} );

				container = document.createElement( 'div' );
				document.body.appendChild( container );
				container.appendChild( renderer.domElement );

				stats = new Stats();
				container.appendChild( stats.dom );

				window.addEventListener( 'resize', onWindowResize, false );

				window.addEventListener('keydown', keyboard);

			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();
				renderer.setSize( window.innerWidth, window.innerHeight );
				controls.handleResize();

			}

			function keyboard ( ev ) {

				var ZaghettoMesh = scene.getObjectByName( "Zaghetto.pcd" );

				switch ( ev.key ) {

					case '+':
						ZaghettoMesh.material.size*=1.2;
						ZaghettoMesh.material.needsUpdate = true;
						break;

					case '-':
						ZaghettoMesh.material.size/=1.2;
						ZaghettoMesh.material.needsUpdate = true;
						break;

					case 'c':
						ZaghettoMesh.material.color.setHex(Math.random()*0xffffff);
						ZaghettoMesh.material.needsUpdate = true;
						break;

				}

			}

			function animate() {

				requestAnimationFrame( animate );
				controls.update();
				renderer.render( scene, camera );
				stats.update();

			}

		</script>
	</body>
</html>
